Skip to content

fix(spanner): preserve emulator_host and labels in Instance.copy() - #19302

Merged
sakthivelmanii merged 3 commits into
mainfrom
fix/spanner-instance-copy-emulator-host-labels
Dec 2, 2026
Merged

sakthivelmanii merged 3 commits into
mainfrom
fix/spanner-instance-copy-emulator-host-labels

Conversation

@sakthivelmanii

Copy link
Copy Markdown
Contributor

Problem Statement

  • Calling Instance.copy() on either the synchronous or asynchronous Spanner Instance omits emulator_host and labels.
  • When using the Spanner emulator (SPANNER_EMULATOR_HOST or AnonymousCredentials with a custom api_endpoint), admin calls on the copied instance target the emulator while data-plane calls through Database fall back to spanner.googleapis.com.
  • Dropping labels on copy also causes instance.copy().update() to clear existing server-side labels and instance.copy().create() to omit the original labels.

Solution

  • Preserve emulator_host and shallow-copy labels into a fresh dict in synchronous and asynchronous Instance.copy(), so copied instances keep emulator data-plane routing and retain non-aliased labels across create() and update() calls.
  • Behavior changes:
    • instance.copy().update() now preserves existing labels instead of clearing them.
    • instance.copy().create() now sends the original labels.
    • Under emulator configuration, data-plane RPCs on a copied instance now route to the emulator rather than spanner.googleapis.com.
  • Add regression coverage verifying sync and async Client.copy() and Instance.copy() preserve programmatic universe_domain, emulator_host, and labels without aliasing.

## Problem Statement

- Calling `Instance.copy()` on either the synchronous or asynchronous Spanner `Instance` omits `emulator_host` and `labels`.
- When using the Spanner emulator (`SPANNER_EMULATOR_HOST` or `AnonymousCredentials` with a custom `api_endpoint`), admin calls on the copied instance target the emulator while data-plane calls through `Database` fall back to `spanner.googleapis.com`.
- Dropping `labels` on copy also causes `instance.copy().update()` to clear existing server-side labels and `instance.copy().create()` to omit the original labels.

## Solution

- Preserve `emulator_host` and shallow-copy `labels` into a fresh `dict` in synchronous and asynchronous `Instance.copy()`, so copied instances keep emulator data-plane routing and retain non-aliased labels across `create()` and `update()` calls.
- Behavior changes:
  - `instance.copy().update()` now preserves existing labels instead of clearing them.
  - `instance.copy().create()` now sends the original labels.
  - Under emulator configuration, data-plane RPCs on a copied instance now route to the emulator rather than `spanner.googleapis.com`.
- Add regression coverage verifying sync and async `Client.copy()` and `Instance.copy()` preserve programmatic `universe_domain`, `emulator_host`, and `labels` without aliasing.
@sakthivelmanii
sakthivelmanii requested a review from a team as a code owner December 2, 2026 07:00

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the copy() method in both the synchronous and asynchronous Instance classes to preserve the emulator_host and a shallow copy of labels. It also adds comprehensive unit tests to verify that the universe domain, emulator host, and labels are correctly preserved when copying clients and instances. The review feedback highlights a potential issue where converting None labels to an empty dictionary could unintentionally clear labels on the server during updates. The reviewer suggests preserving None values for labels in both Instance implementations and updating the corresponding test cases to verify this behavior.

@olavloite olavloite left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the two duplicated tests, otherwise LGTM

@sakthivelmanii
sakthivelmanii enabled auto-merge (squash) December 2, 2026 15:48
@sakthivelmanii
sakthivelmanii merged commit a601f5f into main Dec 2, 2026
85 checks passed
@sakthivelmanii
sakthivelmanii deleted the fix/spanner-instance-copy-emulator-host-labels branch December 2, 2026 16:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants